package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2019/1/21 15:53
 * @description
 */
public class Code134 {
    public static void main(String[] args) {
        ///System.out.println(new Code134().canCompleteCircuit(new int[] {5, 1, 2, 3, 4}, new int[] {4, 4, 1, 5, 1}));
        System.out.println(new Code134().canCompleteCircuit(new int[] {2}, new int[] {2}));
    }

    public int canCompleteCircuit(int[] gas, int[] cost) {
        int length = gas.length;
        for (int i = 0; i < length; i++) {
            if (gas[i] >= cost[i]) {
                int index = i;
                int total = 0;
                for (int j = 0; j < length; j++) {
                    if (index >= length) {
                        index = 0;
                    }
                    total += gas[index] - cost[index];
                    if (total < 0) {
                        break;
                    }
                    index++;
                }
                if (total >= 0) {
                    return i;
                }
            }
        }
        return -1;
    }
}
